<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>物流配送管理系统</h1>
        <p>欢迎登录</p>
      </div>
      
      <a-form
        :model="formData"
        :rules="rules"
        @finish="handleSubmit"
      >
        <!-- 租户编码（可选，平台管理员无需填写） -->
        <a-form-item name="tenantCode">
          <a-input
            v-model:value="formData.tenantCode"
            size="large"
            placeholder="租户编码（平台管理员无需填写）"
          >
            <template #prefix>
              <TeamOutlined />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item name="username">
          <a-input
            v-model:value="formData.username"
            size="large"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item name="password">
          <a-input-password
            v-model:value="formData.password"
            size="large"
            placeholder="请输入密码"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            block
            :loading="loading"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  UserOutlined,
  LockOutlined,
  TeamOutlined,
} from '@ant-design/icons-vue'
import { useUserStore } from '@/stores/user'
import { login } from '@/api/auth'
import type { LoginParams } from '@/types/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)

const formData = reactive<LoginParams>({
  username: '',
  password: '',
  tenantCode: '',
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' },
  ],
}

// 提交登录
const handleSubmit = async () => {
  loading.value = true
  try {
    const response = await login(formData)
    const { token, userInfo } = response.data
    
    // 保存登录信息
    userStore.login(token, userInfo)
    
    message.success('登录成功')
    
    // 根据角色智能跳转
    const isPlatform = userInfo.role === 'PLATFORM_ADMIN'
    router.push(isPlatform ? '/platform/dashboard' : '/tenant/dashboard')
  } catch (error: any) {
    // 业务错误由页面显示，系统错误已在拦截器中显示
    console.error('Login error:', error)
    // 只显示业务错误（后端返回的 message）
    if (error.message && error.message !== 'Error') {
      message.error(error.message)
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.login-container::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
  border-radius: 50%;
}

.login-container::after {
  content: '';
  position: absolute;
  bottom: -30%;
  left: -10%;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.08) 0%, transparent 70%);
  border-radius: 50%;
}

.login-box {
  width: 100%;
  max-width: 420px;
  padding: 48px 40px;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  border-radius: var(--radius-xl);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 1;
  animation: slideUp 0.5s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 36px;
}

.login-header h1 {
  font-size: 28px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.login-header p {
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 400;
}

/* 表单项间距 */
:deep(.ant-form-item) {
  margin-bottom: 20px;
}

/* 输入框样式增强 */
:deep(.ant-input),
:deep(.ant-input-password) {
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border-color);
  transition: all var(--transition-base);
}

:deep(.ant-input:hover),
:deep(.ant-input-password:hover) {
  border-color: var(--primary-color);
}

:deep(.ant-input:focus),
:deep(.ant-input-password:focus),
:deep(.ant-input-affix-wrapper-focused) {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

:deep(.ant-input-affix-wrapper) {
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border-color);
}

/* 输入框前缀图标 */
:deep(.ant-input-prefix) {
  color: var(--text-tertiary);
  margin-right: 12px;
}

/* 登录按钮 */
:deep(.ant-btn-primary) {
  font-size: 15px;
  font-weight: 500;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
  transition: all var(--transition-base);
}

:deep(.ant-btn-primary:hover) {
  background: linear-gradient(135deg, var(--primary-hover) 0%, var(--primary-active) 100%);
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
  transform: translateY(-2px);
}

:deep(.ant-btn-primary:active) {
  transform: translateY(0);
}

.login-tips {
  margin-top: 16px;
  text-align: center;
}

.login-tips p {
  font-size: 13px;
  color: var(--text-tertiary);
  margin: 0;
  line-height: 1.6;
}
</style>

